<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 我的借阅信息 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.id" placeholder="借阅编号" class="handle-input mr10" style="width: 100px"></el-input>
                <el-input v-model="query.bookId" placeholder="书籍编号" class="handle-input mr10" style="width: 100px"></el-input>
                <el-select v-model="query.isEnd" placeholder="是否归还" class="handle-select mr10" clearable>
                    <el-option key="1" label="是" value="1"></el-option>
                    <el-option key="2" label="否" value="0"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="borrowList" border class="table" header-cell-class-name="table-header">
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="book.id" label="书籍编号" width="150"></el-table-column>
                <el-table-column prop="book.title" label="书籍标题"></el-table-column>
                <el-table-column prop="book.type" label="书籍所属类别"></el-table-column>

                <el-table-column prop="startDate" label="起始时间"></el-table-column>
                <el-table-column label="归还日期" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.isEnd==1?scope.row.endDate:'未归还' }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="书籍封面（查看大图）" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.book.coverUrl" :preview-src-list="[scope.row.book.coverUrl]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-discount" @click="returnBook(scope.$index, scope.row)" v-if="scope.row.isEnd==0">归还书籍</el-button>
                        <el-tag type="success" v-if="scope.row.isEnd==1">书籍已归还</el-tag>
                    </template>
                </el-table-column>
            </el-table>
            
        </div>

        
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            query:{
                id:'',
                bookId:'',
                isEnd:''
            },
            bookId:'',
            borrowList: [],
            idx: -1,
        };
    },
    created() {
        this.handleSearch();
    },
    methods: {
        // 触发搜索按钮
        handleSearch() {
            this.$http.post('/book/getborrowlistbyaccount', this.query).then((res) => {
                this.borrowList = res.data.data;
            });
        },
        // 编辑操作
        returnBook(index, row) {
            this.idx = index;
            this.$http.get("/book/return/"+row.book.id).then(res => {
                if(res.data.status==1){
                    console.log(res.data.data);
                    this.$set(this.borrowList, index, res.data.data);
                }else{
                    this.$message.error("书籍归还失败！");
                }
            });
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
